// pages/my/watch_history/watch_history.js
const appInstance = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isshow: false,
    show: true,
    hide: true,
    hiddenn: false,
    date: true,
    quanxuan: '全选',
    num: 0,
    checkedList: [],
    aaaa: false,
    editors_box: true,
    delete_box: false,
    container: true,
    Today_videos: [],
    His_videos: [],
    Yes_videos: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getWatchList()
  },

  //观看历史列表
  getWatchList() {
    var that = this;
    wx.request({
      url: appInstance.globalData.url + '/user/listHistory',
      method: "POST",
      data: {
        pp_token: wx.getStorageSync('to_ken')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data.today_videos)
        // console.log(res.data.data.his_videos)
        // console.log(res.data.data.yes_videos)
        that.setData({
          Today_videos: res.data.data.today_videos,
          His_videos: res.data.data.his_videos,
          Yes_videos: res.data.data.yes_videos
        })
      }
    })

  },
  //checkbox选中事件
  onChange(e) {
    var id = e.currentTarget.id
    // 今天
    var Today_videosList = this.data.Today_videos
    // console.log(list)
    Today_videosList.forEach(item => {
      if (item.video_id == id) {
        item.checked = !item.checked
      }
    })
    //昨天
    var yesTodayList = this.data.Yes_videos
    yesTodayList.forEach(item => {
      if (item.video_id == id) {
        item.checked = !item.checked
      }
    })
    //更早
    var hisTodayList = this.data.His_videos
    hisTodayList.forEach(item => {
      if (item.video_id == id) {
        item.checked = !item.checked
      }
    })
    //数组拼接
    var newArr = [].concat(Today_videosList, yesTodayList, hisTodayList)
    // console.log(newArr)

    //过滤选中的
    var checkedList = newArr.filter(item => {
      return item.checked
    })

    this.setData({
      Today_videos:Today_videosList,
      His_videos: hisTodayList,
      Yes_videos: yesTodayList,
      //Newlist是过滤的
      checkedList: checkedList
    })




  },
  //编辑
  editors() {
    var that = this;
    that.setData({
      isshow: true,
      show: false,
      hide: true,
    })
  },
  //取消
  quxiaoTap() {
    var that = this;
    that.setData({
      isshow: false,
      show: true,
      hide: false,
    })
  },
  //全选
  quanxuanTap() {
    var that = this;
    if (that.data.quanxuan == '全选') {
      //今天
      var newList = this.data.Today_videos
      newList.forEach(item => {
        item.checked = true
      })
      //昨天
      var yesTodayList = this.data.Yes_videos
      yesTodayList.forEach(item => {
        item.checked = true
      })
      //最早
      var hisTodayList = this.data.His_videos
      hisTodayList.forEach(item => {
        item.checked = true
      })
      //过滤全选
      var checkedList = newList.filter(item => {
        return item.checked
      })
      that.setData({
        Today_videos: newList,
        Yes_videos: yesTodayList,
        His_videos: hisTodayList,
        quanxuan: '取消全选',
        checkedList: checkedList
      })
    } else if (that.data.quanxuan == '取消全选') {
      //今天
      var newList = this.data.Today_videos
      newList.forEach(item => {
        item.checked = false
      })
      //昨天
      var yesTodayList = this.data.Yes_videos
      yesTodayList.forEach(item => {
        item.checked = false
      })
      //最早
      var hisTodayList = this.data.His_videos
      hisTodayList.forEach(item => {
        item.checked = false
      })
      //过滤全不选
      var checkedList = newList.filter(item => {
        return item.checked
      })
      that.setData({
        Today_videos: newList,
        Yes_videos: yesTodayList,
        His_videos: hisTodayList,
        quanxuan: '全选',
        checkedList: checkedList
      })
    }
  },

  //删除
  delTap() {
    var that = this;
    var list = this.data.checkedList
    // console.log(list)
    var delListId = list.map(item => {
      return item.video_id
    })
    var id = delListId.join(',')
    // console.log(id)
    wx.request({
      url: appInstance.globalData.url + '/user/deleteHistory',
      method: "POST",
      data: {
        pp_token: wx.getStorageSync('to_ken'),
        video_ids: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data)
        if (res.data.code == 200) {
          that.setData({
            show: true,
            isshow: false
          })
          if (getCurrentPages().length != 0) {
            //刷新当前页面的数据
            getCurrentPages()[getCurrentPages().length - 1].onLoad()
          }

        }


      }
    })

  },
  //跳转到详情
  videoDetail(e){
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../../index/classify/videoDetail/videoDetail?id=' + id
    })
  },



})